/**
 * fil
 */

#include <MyProject.h>
#include "pressure.h"

static float convert_adc_to_float(unsigned short adc_data)
{
    float tmp = ((float)adc_data / 4095) * 5;
    return tmp;
}

static float convert_voltage_to_kpa(float v)
{
    return (v * 1.75 - 5.875);
}

/**
 * @brief   根据 ADC 采集的数据计算压力传感器参数
 * @param   sensor --> 压力传感器抽象结构体指针
 */
void calculate_presssure(pressure_sensor_t *sensor)
{
    SetBit(ADC_CR, ADCBSY); // 触发转换

    while (ReadBit(ADC_CR, ADCBSY))
    {
        sensor->adc_value = ADC6_DR >> 3;
    }

    sensor->voltage = convert_adc_to_float(sensor->adc_value);
    sensor->pressure_value = convert_voltage_to_kpa(sensor->voltage);
}

void pressure_sensor_init(pressure_sensor_t *sensor)
{
	sensor->adc_value      = 0;
	sensor->voltage        = 0.0f;
	sensor->pressure_value = 0.0f;
}

